/*
 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_CODEC_DATA
#define OHOS_CODEC_DATA

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>

#include "ohos_video_common.h"

namespace ohos {
class CodecData {
public:
	CodecData();
	~CodecData();
	bool InputReady() const;
	int32_t InputData(const uint8_t *data,
	                  size_t size,
	                  OH_AVCodecBufferAttr *attr,
	                  int *indx,
	                  std::chrono::milliseconds waitTime = (std::chrono::milliseconds)0);
	int32_t NeedInputBuffer(uint32_t index, OH_AVBuffer *buffer);
	uint32_t InputFrame() const;

	bool OutputReady() const;
	int32_t OutputData(AvBufferInfo &buff, std::chrono::milliseconds waitTime = (std::chrono::milliseconds)0);
	int32_t NewOutputBuffer(uint32_t index, OH_AVBuffer *buffer);
	uint32_t OutputFrame() const;

	int32_t GetInputBuffer(AvBufferInfo &buff, std::chrono::milliseconds waitTime = (std::chrono::milliseconds)0);
	int32_t GetOutputBuffer(AvBufferInfo &buff, std::chrono::milliseconds waitTime = (std::chrono::milliseconds)0);

private:
	void Release();

	mutable std::mutex inputMutex_;
	std::condition_variable inputCond_;
	std::queue<AvBufferInfo> inputBufferQueue_;
	uint32_t inputFrameCount_{0};

	mutable std::mutex outputMutex_;
	std::condition_variable outputCond_;
	std::queue<AvBufferInfo> outputBufferQueue_;
	uint32_t outputFrameCount_{0};
};
} // namespace ohos

#endif // OHOS_CODC_DATA
